package demo.practice.array;

public class P724 {

    public static void main(String[] args) {
        P724 p724= new P724();
        p724.pivotIndex(new int[]{-1,-1,0,1,1,-1});
    }

    public int pivotIndex(int[] nums) {

        if(nums==null|| nums.length==0)
            return -1;

        if(nums.length==1)
            return 0;

        int[] store= new int[nums.length+1];

        for(int i=0;i<nums.length;i++){
            store[i+1]= store[i]+ nums[i];
        }

        int sum= 0;
        int res=-1;
        if(sum== store[nums.length-1] )
            res= nums.length-1;


        for(int j=nums.length-1;j-1>=0;j--){
            //sum为 j~nums.length-1
            sum+=nums[j];   //
            if(sum==store[j-1])
                res= j-1;
        }

        return res;

    }
}
